package com.zmn.mcc.admin.controller.staff.staffarchives;


import com.zmn.common.dto.LoginStaff;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.request.RequestUtil;
import com.zmn.mcc.business.interfaces.staff.ip.StaffIpInfoBService;
import com.zmn.mcc.common.constant.StaffChangeLogConsts;
import com.zmn.mcc.model.entity.log.IpInfo;
import com.zmn.mcc.model.entity.staff.staffarchives.StaffArchives;
import com.zmn.mcc.model.entity.staff.staffchangelog.StaffChangeLog;
import com.zmn.mcc.services.interfaces.staff.staffarchives.StaffArchivesService;
import com.zmn.mcc.services.interfaces.staff.staffchangelog.StaffChangeLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;


/**
 * 员工档案基础修改Controller
 *
 * @author tanghongjun
 * @since 2020-05-14 11:15:18
 */
@RestController
@RequestMapping("/staff/staffarchives")
public class StaffArchivesModifyController extends StaffArchivesBaseController {
    private Logger logger = LoggerFactory.getLogger(StaffArchivesListController.class);

    @Resource
    private StaffArchivesService staffArchivesService;
    @Resource
    StaffChangeLogService staffChangeLogService;
    @Resource
    StaffIpInfoBService staffIpInfoBService;

    /**
     * 保存
     */
    @RequestMapping("/addEdit")
//    @RequiresPermissions("staff/staffarchives/addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody StaffArchives staffArchives) {

        Date now = DateUtil.getNow();
        int archivesId = Optional.ofNullable(staffArchives.getArchivesId()).orElse(0);

        try {
            if (archivesId == 0) {
                staffArchives.setCreater(getStaffName());
                staffArchives.setCreateTime(now);
                staffArchives.setUpdater(getStaffName());
                staffArchives.setUpdateTime(now);
                staffArchivesService.insert(staffArchives);
            } else {
                StaffArchives staffArchivesHis = staffArchivesService.findById(archivesId);
                if(!Objects.equals(staffArchivesHis.getBankCardNoVarchar(), staffArchives.getBankCardNoVarchar())){
                    // 添加员工新增日志
                    StaffChangeLog staffChangeLog = new StaffChangeLog();
                    staffChangeLog.setStaffId(staffArchives.getStaffId());
                    staffChangeLog.setBeforContent(staffArchivesHis.getBankCardNoVarchar());
                    staffChangeLog.setAfterContent(staffArchives.getBankCardNoVarchar());
                    LoginStaff staffLogin = getLoginStaff();
                    staffChangeLog.setAbutmenterId(staffLogin.getStaffId());
                    staffChangeLog.setAbutmenter(staffLogin.getRealName());
                    staffChangeLog.setAbutmenterDeptId(staffLogin.getDeptId() + "");
                    staffChangeLog.setAbutmenterDeptName(staffLogin.getDeptName());
                    staffChangeLog.setChangeType(StaffChangeLogConsts.CHANGE_TYPE_EDIT_BANK_NO);
                    staffChangeLog.setCreateTime(DateUtil.getNow());
                    String ip = RequestUtil.getRequestIp(request);
                    IpInfo ipInfo = staffIpInfoBService.locationByIP(ip);
                    staffChangeLog.setIp(ip);
                    if (Objects.nonNull(ipInfo)) {
                        staffChangeLog.setAbutmenterCity(ipInfo.getProvince() + "-" + ipInfo.getCity());
                    }
                    staffChangeLogService.insert(staffChangeLog);
                }
                staffArchives.setUpdater(getStaffName());
                staffArchives.setUpdateTime(now);
                staffArchivesService.updateStaffArchivesByKey(staffArchives);
            }

            return ResponseDTO.success("操作成功！");
        } catch (Exception e) {
            logger.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 删除
     *
     * @param archivesId
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
//    @RequiresPermissions("staff/staffarchives/delete")
    public ResponseDTO delete(@RequestParam(value = "archivesId", defaultValue = "0") Integer archivesId) {
        if (NumberUtil.isNullOrZero(archivesId)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }
        staffArchivesService.deleteByKey(archivesId);
        return ResponseDTO.success("删除成功！");
    }

}
